<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经济带" prop="economic">
        <el-select v-model="queryParams.economic" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.economic_belt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布频道" prop="spaceType">
        <el-select v-model="queryParams.spaceType"
                   placeholder="请选择发布频道">
          <el-option
            v-for="dict in dict.type.space_type"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布状态" prop="reserveState">
        <el-select v-model="queryParams.reserveState" placeholder="请选择" clearable>
          <el-option
            v-for="dict in reserveStates"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-select v-model="queryParams.sort" clearable placeholder="请选择">
          <el-option
            v-for="dict in sortType"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['information:park:add']"
        >修订关联
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:park:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkList" @selection-change="handleSelectionChange" height="600" fixed>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户名称" align="center" prop="nickName">
        <template slot-scope="scope">
          <div v-for="item in userList" v-if="item.userId === scope.row.userId">
            <span>{{ item.nickName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center" prop="userId"/>
      <el-table-column label="角色" align="center" prop="userType">
        <template slot-scope="scope">
          <div v-for="item in userList" v-if="item.userId === scope.row.userId">
            <div v-for=" items in userTypes" v-if="item.userType === items.value">
              <span>{{ items.label }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="发布频道" align="center" prop="spaceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.space_type" :value="scope.row.spaceType"/>
        </template>
      </el-table-column>
      <el-table-column label="关联园区名称" align="center" prop="pkName"/>
      <el-table-column label="发布状态" align="center" prop="reserveState">
        <template slot-scope="scope">
          <div v-for="item in reserveStates" v-if="item.value === scope.row.reserveState">
            <span>{{ item.label }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="审核" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <div v-for="item in status" v-if="item.value === scope.row.auditStatus">
            <span>{{ item.label }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="下架状态" align="center" prop="state">
        <template slot-scope="scope">
          <div v-for="item in status2" v-if="item.value === scope.row.state">
            <span>{{ item.label }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column width="250" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.auditStatus==0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['information:park:edit']"
          >审核
          </el-button>
          <el-button
            v-if="scope.row.state===1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleState(scope.row)"
            v-hasPermi="['information:park:edit']"
          >上架
          </el-button>
          <el-button
            v-if="scope.row.state===0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleState(scope.row)"
            v-hasPermi="['information:park:edit']"
          >下架
          </el-button>
          <el-button
            v-if="scope.row.reserveState===2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReserveState(scope.row)"
            v-hasPermi="['information:park:edit']"
          >预定
          </el-button>
          <el-button
            v-if="scope.row.reserveState===1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReserveState(scope.row)"
            v-hasPermi="['information:park:edit']"
          >取消预订
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:park:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预选产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="759px" :close-on-click-modal='false'  append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="用户手机" prop="userId">
          <el-select class="kind" v-model="form.userId" @change="changeUserId" filterable placeholder="请选择用户">
            <el-option
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.phonenumber"
              :value="dict.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input class="kind" v-model="form.id2" disabled placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="用户名称" prop="userId">
          <el-input class="kind" v-model="form.name" disabled placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="发布频道" prop="spaceType">
          <el-select class="kind" v-model="form.spaceType" @change="changeSpaceType"
                     placeholder="请选择发布频道">
            <el-option
              v-for="dict in dict.type.space_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联园区" prop="pkId">
          <el-select class="kind" v-model="form.pkId" @change="changePkId" placeholder="请选择">
            <el-option
              v-for="dict in selectList"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="摘要" prop="note">
          <el-input class="kind" v-model="form.note" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核" :visible.sync="open2" width="500px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="状态" prop="auditStatus">
          <el-select  v-model="form.auditStatus" placeholder="请选择状态">
            <el-option
              v-for="dict in status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input style="width: 213px" v-model="form.note" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addPark, delPark, getPark, listAllPark, listPark, updatePark} from "@/api/information/park";
import {getUser, listUserAll} from "@/api/information/user";

export default {
  name: "Park",
  dicts: ['space_type','economic_belt'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预选产品表格数据
      parkList: [],
      selectList: [],
      userList: [],
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      userTypes: [
        {
          value: "01",
          label: "供给方政府"
        },
        {
          value: "02",
          label: "供给方产权单位"
        },
        {
          value: "03",
          label: "供给方运营单位"
        },
        {
          value: "04",
          label: "需求方"
        },
      ],
      status: [
        {
          value: 0,
          label: "待审批"
        },
        {
          value: 1,
          label: "审批通过"
        },
        {
          value: 2,
          label: "审批失败"
        },
      ],
      status2: [
        {
          value: 0,
          label: "否"
        },
        {
          value: 1,
          label: "是"
        },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        spaceType: null,
        pkId: null,
        pkName: null,
        reserveState: null,
        phonenumber: null,
        note: null,
        sort: null,
        state: null,
        dr: null,
        nickName:null,
        economic:null,
        province:null,
      },
      list: {
        parkList: [],
        buildingCommercialList: [],
        buildingOfficeList: [],
        warehouseList: [],
        workShopList: [],
        apartmentList: [],
        auctionList: [],
        landList: []
      },
      // 表单参数
      form: {},
      reserveStates: [
        {
          value: 1,
          label: "已预定"
        },
        {
          value: 2,
          label: "未预定"
        },
      ],
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "用户不能为空", trigger: "blur"}
        ],
        spaceType: [
          {
            required: true,
            message: "发布频道不能为空",
            trigger: "change"
          }
        ],
        pkId: [
          {required: true, message: "园区不能为空", trigger: "blur"}
        ],
        pkName: [
          {required: true, message: "园区名称不能为空", trigger: "blur"}
        ],
        reserveState: [
          {required: true, message: "预定状态不能为空", trigger: "blur"}
        ],
        sort: [
          {required: true, message: "排序不能为空", trigger: "blur"}
        ],
        note: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSpace();
    this.getUserList();
  },
  methods: {
    changeUserId() {
      this.userList.forEach(item => {
        if (item.userId === this.form.userId) {
          this.form.id2=item.userId
          this.form.name = item.nickName
        }
      })
    },


    changePkId() {
      this.selectList.forEach(item => {
        if (item.value === this.form.pkId) {
          this.form.pkName = item.label
        }
      })
    },
    getUserList() {
      listUserAll().then(response => {
        this.userList = response.data
      })
    },
    changeSpaceType() {
      switch (this.form.spaceType) {
        case 1 :
          this.selectList = this.list.parkList;
          break;
        case 2 :
          this.selectList = this.list.buildingCommercialList;
          break;
        case 3 :
          this.selectList = this.list.buildingOfficeList;
          break;
        case 4 :
          this.selectList = this.list.warehouseList;
          break;
        case 5 :
          this.selectList = this.list.workShopList;
          break;
        case 6 :
          this.selectList = this.list.apartmentList;
          break;
        case 7 :
          this.selectList = this.list.auctionList;
          break;
        case 8 :
          this.selectList = this.list.landList;
          break;
      }
    },
    getSpace() {
      listAllPark().then(res => {
        this.list.parkList = res.park
        this.list.buildingCommercialList = res.buildingCommercial
        this.list.buildingOfficeList = res.buildingOffice
        this.list.landList = res.land
        this.list.warehouseList = res.warehouse
        this.list.workShopList = res.workShop
        this.list.apartmentList = res.apartment
        this.list.auctionList = res.auction
      });
    },
    /** 查询预选产品列表 */
    getList() {
      this.loading = true;
      listPark(this.queryParams).then(response => {
        this.parkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        spaceType: null,
        pkId: null,
        pkName: null,
        reserveState: null,
        note: null,
        sort: null,
        state: null,
        dr: null,
        createTime: null,
        updateTime: null,
        name: null,
        id2: null,
        auditStatus:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.reset();
      const id = row.id
      getPark(id).then(response => {
        this.form = response.data;
        this.open2 = true;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预选产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPark(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预选产品";
      });
    },

    /** 修改按钮操作 */
    handleReserveState(row) {
      const id = row.id || this.ids
      if (row.reserveState === 2) {
        this.$modal.confirm('是否确认预订编号为"' + id + '"的数据项？').then(function () {
          row.reserveState = 1
          return updatePark(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("预定成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否确认取消预定编号为"' + id + '"的数据项？').then(function () {
          row.reserveState = 2
          return updatePark(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消预订成功");
        }).catch(() => {
        });
      }
    },
    /** 修改按钮操作 */
    handleState(row) {
      const id = row.id || this.ids
      if (row.state === 1) {
        this.$modal.confirm('是否确认上架编号为"' + id + '"的数据项？').then(function () {
          row.state = 0
          return updatePark(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("上架成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否确认下架编号为"' + id + '"的数据项？').then(function () {
          row.state = 1
          return updatePark(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("下架成功");
        }).catch(() => {
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePark(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.open2 = false;
              this.getList();
            });
          } else {
            addPark(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预选产品编号为"' + ids + '"的数据项？').then(function () {
        return delPark(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/informfrontend/parkBack/export', {
        ...this.queryParams
      }, `park_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.kind {
  width: 90%;
}
</style>
